import Util from "../Utils/Util";
import { CircleGeometry, Color, MathUtils, Mesh, MeshBasicMaterial, OrthographicCamera, RingGeometry, Vector2 } from "three";

export default class Hud {
  constructor() {
    this.InitHudCamera();
    this.InitScene();
  }

  aovSize = 120 * window.devicePixelRatio;
  InitHudCamera() {
    let aspect = window.innerWidth / window.innerHeight;
    render.hudCamera = new OrthographicCamera(
      (this.aovSize * aspect) / -2,
      (this.aovSize * aspect) / 2,
      this.aovSize / 2,
      this.aovSize / -2,
      0.01,
      4000
    );
    render.hudCamera.position.copy(render.mainCamera.position);
    render.hudCamera.updateProjectionMatrix();
  }

  InitScene() {
    this.CreateRocker();
  }

  rocker: Mesh<RingGeometry, MeshBasicMaterial>;
  circleRadis: number;
  rockerRadis: number;
  CreateRocker() {
    this.rockerRadis = 18;
    let ring = new RingGeometry(this.rockerRadis - 0.6, this.rockerRadis, 32);
    let ringMater = new MeshBasicMaterial({ color: new Color("white") });
    this.rocker = new Mesh(ring, ringMater);
    let y = -this.aovSize / 2;
    this.rocker.position.y = y + Math.abs(y) / 2.5;

    let circle = new CircleGeometry(this.rockerRadis / 4, 32);
    let circleMater = new MeshBasicMaterial({ color: new Color("white") });
    let circleMesh = new Mesh(circle, circleMater);
    this.rocker.add(circleMesh);

    this.circleRadis = this.rockerRadis / 4;

    render.hudScene.add(this.rocker);
  }

  oldEvent: TouchEvent;
  distance: number;
  RockerControl(e: TouchEvent) {
    switch (e.type) {
      case "touchstart":
        if (Util.pointer.y < -0.34) {
          this.rocker.position.x = Util.pointer.x * render.hudCamera.right;
          this.rocker.position.y = Util.pointer.y * render.hudCamera.top;

          this.oldEvent = e;
          render.addClockEvent("player_move", this.MovePlayer.bind(this));
        }
        break;
      case "touchmove":
        if (!this.oldEvent) break;
        let x = Util.pointer.x * render.hudCamera.right - this.rocker.position.x;
        let y = Util.pointer.y * render.hudCamera.top - this.rocker.position.y;

        let pos = new Vector2(x, y);
        this.distance = pos.distanceTo(new Vector2(0, 0));
        if (this.distance > this.rockerRadis) {
          pos.multiplyScalar(this.rockerRadis / this.distance);
        }
        this.rocker.children[0].position.x = pos.x;
        this.rocker.children[0].position.y = pos.y;

        let radians = Math.atan2(pos.y, pos.x);
        if (radians < 0) {
          radians += 2 * Math.PI;
        }
        radians = (radians * 180) / Math.PI;

        world.troop.group.userData.angle = radians;
        world.troop.group.rotation.y = MathUtils.degToRad(radians - MathUtils.radToDeg(Math.PI / 2));

        break;
      case "touchend":
        let ny = -this.aovSize / 2;
        this.rocker.position.set(0, ny + Math.abs(ny) / 2, 0);
        this.rocker.children[0].position.set(0, 0, 0);
        this.distance = 0;
        render.removeClockEvent("player_move");
        this.oldEvent = null;
        break;
    }
  }

  MovePlayer() {
    if (this.distance) {
      world.troop.Forward();
    }
  }
}
